package chapter13.eg1.listener;

import chapter13.eg1.broadcaster.LogEvent;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class LogEventHandler extends SimpleChannelInboundHandler<LogEvent> {

  @Override
  protected void channelRead0(ChannelHandlerContext ctx, LogEvent event) throws Exception {
    StringBuilder builder = new StringBuilder();
    builder.append(event.getReceived())
        .append("[")
        .append(event.getSource().toString())
        .append("] [")
        .append(event.getLogFile())
        .append("]: ")
        .append(event.getMsg());
    System.out.println(builder);
  }

  @Override
  public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    cause.printStackTrace();
    ctx.close();
  }
}
